//
// Created by enemy1205 on 2021/8/10.
//
#include "identity.h"

void Manager::addAccount() {
    cout << "请输入添加账号的类型" << endl;
    cout << "1、添加学生" << endl;
    cout << "2、添加老师" << endl;
    string fileName;
    ofstream ofs;
    int select = 0;
    cin >> select;
    int id;
    if (select == 1) {
        fileName = STUDENT_FILE;
        cout << "请输入学号： " << endl;
        L1:
        cin >> id;
        if (check_repeated(vStu, id)) {
            cout << "该账号已存在,请重新输入学号" << endl;
            goto L1;
        }
    } else {
        fileName = TEACHER_FILE;
        cout << "请输入职工编号：" << endl;
        L2:
        cin >> id;
        if (check_repeated(vTea, id)) {
            cout << "该账号已存在,请重新输入学号" << endl;
            goto L2;
        }
    }
    ofs.open(fileName, ios::out | ios::app);
    string name;
    string pwd;
    cout << "请输入姓名： " << endl;
    cin >> name;

    cout << "请输入密码： " << endl;
    cin >> pwd;

    ofs << endl << id << " " << name << " " << pwd;
    cout << "添加成功" << endl;
    getchar();
    ofs.close();
    /*及时将新添加的成员加入查重队列,此处重新计数必须位于ofs.close()后,close后文件才写入保存，才能被读取到*/
    this->count_num();
}

void printStudent(student_re &s) {
    cout << "学号： " << s.get_num() << " 姓名： " << s.get_name() << " 密码：" << s.get_passport() << endl;
}

void printTeacher(teacher &t) {
    cout << "职工号： " << t.get_num() << " 姓名： " << t.get_name() << " 密码：" << t.get_passport() << endl;
}

void Manager::showAccount() {
    cout << "请选择查看内容：" << endl;
    cout << "1、查看所有学生" << endl;
    cout << "2、查看所有老师" << endl;
    int select = 0;
    cin >> select;
    if (select == 1) {
        cout << "所有学生信息如下： " << endl;
        for_each(vStu.begin(), vStu.end(), printStudent);
    } else {
        cout << "所有老师信息如下： " << endl;
        for_each(vTea.begin(), vTea.end(), printTeacher);
    }
    getchar();
    getchar();
    printf("\33[2J");
}

void Manager::showComputer() {
    cout << "机房信息如下： " << endl;
    for (vector<ComputerRoom>::iterator it = vCom.begin(); it != vCom.end(); it++) {
        cout << "机房编号： " << it->get_id() << " 机房最大容量： " << it->get_num() << endl;
    }
    getchar();
}

void Manager::cleanLog() {
    ofstream ofs(ORDER_FILE, ios::trunc);
    ofs.close();
    cout << "清空成功！" << endl;
    getchar();
}

/*计数时过滤掉已存在重复*/
template<typename T>
void Manager::check_repeated(vector<T> &t) {
    for (auto it = t.begin(); it != t.end(); it++) {
        for (auto it2 = it + 1; it2 != t.end(); it2++) {
            if (it->get_name() != it2->get_name()) continue;
            else {
                t.erase(it2);
                break;
            }
        }
    }
}

/*添加时比对查重*/
template<typename T>
bool Manager::check_repeated(vector<T> &t, int id) {
    for (auto it = t.begin(); it != t.end(); it++) {
        if (it->get_num() == id) return true;
    }
    return false;
//    class A{
//    public:int a_id;
//    };
//    class B{
//    public:int b_id;
//    };
//    vector<A>_A;
//    vector<B>_B;
//    int ID;
//    template<typename T>
//    T t;
//    find_if(t.begin(),t.end(),)
}

void Manager::count_num() {
    vStu.clear();
    vTea.clear();
    vCom.clear();
    //读取学生文件中信息
    ifstream ifs;
    ifs.open(STUDENT_FILE, ios::in);
    if (!ifs.is_open()) cout << "学生文件打开错误" << endl;
//    ifs.seekg(17,ios::beg);/*略去第一行*/
    student_re s;
    while (ifs >> s.get_num() && ifs >> s.get_name() && ifs >> s.get_passport()) {
            vStu.push_back(s);
            this->check_repeated(vStu);
    }
    ifs.close(); //学生初始化
    ifs.clear(ios::goodbit);/*读取多个文件时必须先清空流状态*/
    //读取老师文件信息
    ifs.open(TEACHER_FILE, ios::in);
    if (!ifs.is_open()) cout << "教师文件打开错误" << endl;
//    ifs.seekg(17,ios::beg);/*略去第一行*/
    teacher t;
    while (ifs >> t.get_num() && ifs >> t.get_name() && ifs >> t.get_passport()) {
        vTea.push_back(t);
        this->check_repeated(vTea);
    }
    ifs.close();/*教师初始化*/
    ifs.clear(ios::goodbit);
    /*读取机房信息*/
    ifs.open(COMPUTER_FILE, ios::in);
    if (!ifs.is_open()) cout << "机房文件打开错误" << endl;
    ComputerRoom c;
    while (ifs >> c.get_id() && ifs >> c.get_num()) {
        vCom.push_back(c);
    }
    ifs.close();
}

void Manager::operMenu(const string &name) {
    cout << "欢迎管理员：" << name << "登录！" << endl;
    while (true) {
        cout << "\t\t ---------------------------------\n";
        cout << "\t\t|                                |\n";
        cout << "\t\t|          1.添加账号             |\n";
        cout << "\t\t|                                |\n";
        cout << "\t\t|          2.查看账号             |\n";
        cout << "\t\t|                                |\n";
        cout << "\t\t|          3.查看机房             |\n";
        cout << "\t\t|                                |\n";
        cout << "\t\t|          4.清空预约             |\n";
        cout << "\t\t|                                |\n";
        cout << "\t\t|          0.注销登录             |\n";
        cout << "\t\t|                                |\n";
        cout << "\t\t ---------------------------------\n";
        cout << "请选择您的操作： " << endl;
        int select = 0;
        cin >> select;
        switch (select) {
            {
                case 1: {
                    cout << "添加账号" << endl;
                    this->addAccount();
                    break;
                }
            }
            {
                case 2: {
                    cout << "查看账号" << endl;
                    this->showAccount();
                    break;
                }
            }
            {
                case 3: {
                    cout << "查看机房" << endl;
                    this->showComputer();
                    break;
                }
            }
            {
                case 4: {
                    cout << "清空预约" << endl;
                    this->cleanLog();
                    break;
                }
            }
            {
                case 0: {
                    cout << "注销成功" << endl;
                    return;
                }
            }
        }
    }
}